<template>
  <div class="main-app">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="form-inline">
        <el-form-item label="角色名称:">
          <el-input v-model="formInline.name" clearable />
        </el-form-item>
        <!-- <el-form-item label="组织名称:">
          <el-input v-model="formInline.phone" clearable />
        </el-form-item> -->
        <el-form-item label="状态:">
          <el-select v-model="formInline.disable" placeholder="请选择" clearable>
            <el-option label="启用" value="0" />
            <el-option label="禁用" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">
            查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <div class="show-result">

      <!-- 加载动态菜单 -->
      <el-form :inline="true" class="form-inline">
        <el-form-item v-for="item in toolbtn" :key="item.id">
          <el-button type="primary" :icon="item.meta.icon" @click="showViews(item.name)">
            {{ item.meta.title }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-edit" :disabled="disabled" @click="edit">
            编辑
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-delete-solid" @click="deleteUser">
            删除
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-document-checked" :disabled="disabled" @click="enable('0')">
            启用
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-document-delete" :disabled="disabled" @click="enable('1')">
            禁用
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-s-operation" :disabled="disabled" @click="rightConfig()">
            权限配置
          </el-button>
        </el-form-item>
      </el-form> -->

      <div class="result" style="margin-top: -10px">
        <div class="table">

          <el-table ref="multipleTable" v-loading="loading" :data="tableData" border tooltip-effect="dark"
                    style="width: 100%" @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" />

            <el-table-column prop="name" label="组名称" />
            <el-table-column prop="disabled" label="状态" width="160">
              <template slot-scope="scope">
                <el-tag style="width: 100%; text-align: center;" :class="'bg-'+scope.row.disabledColor" effect="dark">
                  {{ scope.row.disabledName }}
                </el-tag>
              </template>
            </el-table-column>


            <el-table-column prop="remarks" label="备注" />

          </el-table>
        </div>
        <div class="page">
          <el-pagination background :current-page.sync="currentPage" layout="total,prev, pager, next, sizes"
                         :total="total" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @current-change="handleCurrentChange"
                         @size-change="handleSizeChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  pageMixin
} from "../../../mixins/index";
import {
  EquipmentStatus
} from "../../../utils/resource.js";
import {
  GroupLists,
  DisabledGroup
} from '../../../api/group.js';
export default {
  filters: {
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },

  },
  mixins: [pageMixin],
  data() {
    return {
      formInline: {
        name: "",
        phone: "",
        disable: ""
      },
      toolbtn: [] // 列表顶部按钮
    };
  },
  computed: {

  },
  async created() {
    await this.init();
  },
  methods: {
    // 列表顶部功能按钮点击事件
    showViews(action) {
       console.log(action);
      switch (action) {
        case '新增':
          this.add();
          break;
        case '编辑':
          this.edit();
          break;
        case '启用':
           this.enable('0');
          break;
        case '禁用':
          this.enable('1');
          break;

      }
    },
    async init() {
      // 初始化按钮数据
      this.toolbtn = JSON.parse(localStorage.getItem('组管理'));
       console.log(this.toolbtn);
      // return;
      this.tableData = [];
      this.loading = true;
      const {
        data,
        count
      } = await GroupLists({
        limit: this.pageSize,
        page: this.currentPage,
         Name: this.formInline.name,
      });
      this.tableData = data;
   
      this.tableData.forEach(item => {
        var obj = EquipmentStatus.find((p) => p.value === `${item.disabled}`);
        if (obj) {
          item.disabledName = obj.label;
          item.disabledColor = obj.color;
        } else {
          item.disabledName = "无数据";
          item.disabledColor = "red";
        }
      });
      this.total = count;
      this.loading = false;


    },

    async enable(value) {
      const arr = this.$refs.multipleTable.selection;
      if (arr.length < 1) {
        this.$message.error('请至少选择一条数据');
        return;
      }

      let confimText = "";
      if (value === "0") {
        confimText = "启用";
      } else {
        confimText = "禁用";
      }
      this.$confirm(`此操作将所选用户信息状态设置为${confimText}状态, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async() => {
        var params = {
          Ids: arr[0].id,
          Disabled: value
        }

        // 操作数据
        const { success, msg } = await DisabledGroup(params);

        if (success) {
          await this.init();
          this.$notify({
            title: "成功",
            message: `${confimText}状态更新成功!`,
            type: "success",
            duration: 3000,
          });
        } else {
          this.$notify.error({
            title: "失败",
            message: msg,
            duration: 3000,
          });
        }
      });
    },
    edit() {
      const arr = this.$refs.multipleTable.selection;
      if (arr.length !== 1) {
        this.$message.error('此功能仅限操作1条数据！');
      } else {
        // this.$router.push(`/organization/group/${arr[0].id}/edit/${arr[0].name}/sort/${arr[0].sortNo}/disabled/${arr[0].disabled}`);
   this.$router.push(`/organization/group/edit/${arr[0].id}`);
      }
    },
    add() {

      this.$router.push("/organization/group/create");
    },

    rightConfig() {
      const arr = this.$refs.multipleTable.selection;
      if (arr.length !== 1) {
        this.$message.error('此功能仅限操作1条数据！');
      } else {

        this.$router.push(`/organization/group/right/${this.id}`);
      }
    },
    async reset() {
      this.formInline = {
        name: "",
        phone: "",
        disable: "",
      };
      await this.init();
    },

  },
};
</script>

<style lang="scss" scoped>
  .result {
    margin-top: 10px;
  }

  .show-result {
    margin-top: 10px;
  }
</style>
